package com.ebelter.bpm.activitys;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ebelter.bpm.R;
import com.ebelter.bpm.common.Constant;
import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.getEmailValidateCodeResponse;

import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.VerifyUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.CombinView1;
import com.ebelter.btcomlib.views.CountDownTextView;

import static com.ebelter.btcomlib.views.CountDownTextView.LAST_SEND_CODE_TIME;

public class BPM_VerificEmailCodeActivity extends BaseActivity {

    public static final String TAG = "BPM_VerificEmailCodeActivity";


    public static final int MODIFY_PSW = 1;

    int CMD_WHICH = Constant.IBpm.CMD_PHONE;//默认手机号码注册

    TextView topTitleTv;

    CombinView1 email;
    CombinView1 emailCode;
    CountDownTextView countDownTv;
    TextView emailcode_next_bt;


    @Override
    protected int loadLayoutById() {
        return R.layout.activity_bpm_emailcode;
    }



    @Override
    protected void initView() {
        FV();
        //emailcode_next_bt.setEnabled(false);

        countDownTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CMD_WHICH==Constant.IBpm.CMD_PHONE){
                    sendPhoneQuest();
                }else if (CMD_WHICH==Constant.IBpm.CMD_EMAIL){
                    sendEmailQuest();
                }

            }
        });
    }

    private void sendPhoneQuest() {
        String mail=email.getTitle1();
        if (TextUtils.isEmpty(mail)){
            ToastUtil.show("手机号码不能为空");
            return;
        }

        if (!VerifyUtils.isMobile(mail)){
            ToastUtil.show("请输入正确的手机号码");
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        NetUtils.getInstance().getPhoneValidateCodeRequest(this, mail, 2, new HttpResponse<getEmailValidateCodeResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, getEmailValidateCodeResponse getEmailValidateCodeResponse, String s) {
                if (!isRequestSuccess){
                    return;
                }
                if (getEmailValidateCodeResponse.resultCode==0){
                    ToastUtil.show("手机验证码发送成功");
                    countDownTv.click();
                    emailcode_next_bt.setEnabled(true);

                }else if (getEmailValidateCodeResponse.resultCode==104){//邮箱没有注册
                    ToastUtil.show("手机号码没有注册");
                }else if (getEmailValidateCodeResponse.resultCode==110){//服务器异常
                    ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                }else {
                    ToastUtil.show("手机验证码发送失败");
                }
            }
        });

    }


    //请求邮箱验证码
    private void sendEmailQuest() {
        String mail=email.getTitle1();
        if (TextUtils.isEmpty(mail)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }

        if (!VerifyUtils.emailFormat(mail)){
            ToastUtil.show(R.string.Incorrect_email_format);
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        NetUtils.getInstance().getEmailValidateCodeRequest(this, mail, 2, new HttpResponse<getEmailValidateCodeResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, getEmailValidateCodeResponse getEmailValidateCodeResponse, String s) {
                if (!isRequestSuccess){
                    return;
                }
                if (getEmailValidateCodeResponse.resultCode==0){
                    ToastUtil.show(R.string.The_email_has_been_sent);
                    countDownTv.click();
                    emailcode_next_bt.setEnabled(true);

                }else if (getEmailValidateCodeResponse.resultCode==104){//邮箱没有注册
                    ToastUtil.show(R.string.Email_is_not_registered);
                }else if (getEmailValidateCodeResponse.resultCode==110){//服务器异常
                    ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                }else {
                    ToastUtil.show(R.string.Failed_to_send_mail);
                }
            }
        });



    }

    private void FV() {
        topTitleTv=findViewById(R.id.top_title_tv);
        email = findViewById(R.id.cb1_bpm_email);
        emailCode = findViewById(R.id.cb1_bpm_emailcode);
        countDownTv = findViewById(R.id.countDownTv);
        emailcode_next_bt = findViewById(R.id.emailcode_next_bt);

    }

    @Override
    protected void initData() {
       // topTitleTv.setText(R.string.str_About);
        SpUtil.writeLong(LAST_SEND_CODE_TIME,0);
        adaptedVersion();



    }

    private void adaptedVersion() {

        if (AppUtils.getLocalStr().equals(AppUtils.zh)){
            //中国版本
            CMD_WHICH= Constant.IBpm.CMD_PHONE;
            email.setHint1("手机号码");
            emailCode.setHint1("手机验证码");
            email.setTittle1InputType(0);
            email.setTitle1(UserSpUtil.readString(Constants.IUser.PHONE));


        }else {
            //国外版本
            CMD_WHICH= Constant.IBpm.CMD_EMAIL;
            email.setHint1(CommonLib.getString(R.string.str_E_mail));//邮箱
            emailCode.setHint1(CommonLib.getString(R.string.email_verification_code));
            email.setTittle1InputType(3);
            email.setTitle1(UserSpUtil.readString(Constants.IUser.EMAIL));

        }


    }

    //返回键被按下
    public void backClick(View view){
        LogUtils.i(TAG,"返回键被按下");
        finish();
    }



    //下一步按钮
    public void nextBtClick(View view){
        LogUtils.i(TAG,"nextBtClick");


        if (CMD_WHICH==Constant.IBpm.CMD_PHONE){
            nextPhone();
        }else if (CMD_WHICH==Constant.IBpm.CMD_EMAIL){
            nextEmail();
        }


    }

    private void nextEmail() {
        String email2=email.getTitle1();
        if (TextUtils.isEmpty(email2)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }
        if (!VerifyUtils.emailFormat(email2)){
            ToastUtil.show(R.string.Incorrect_email_format);
            return;
        }
        String emailCode2=emailCode.getTitle1();
        if (TextUtils.isEmpty(emailCode2)){
            ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
            return;
        }

        Intent intent=new Intent(this,BPM_ModifyPswActivity.class);
        intent.putExtra(BPM_ModifyPswActivity.KEY_ACTION,BPM_ModifyPswActivity.MODIFY_PSW);
        intent.putExtra(BPM_ModifyPswActivity.KEY_CMD,Constant.IBpm.CMD_EMAIL);
        intent.putExtra(BPM_ModifyPswActivity.KEY_EMAIL,email2);
        intent.putExtra(BPM_ModifyPswActivity.KEY_EMAILCODE,emailCode2);
        startActivity(intent);
    }

    private void nextPhone() {
        String phone=email.getTitle1();
        if (TextUtils.isEmpty(phone)){
            ToastUtil.show("手机号码不能为空");
            return;
        }
        if (!VerifyUtils.isMobile(phone)){
            ToastUtil.show("请输入正确的手机号码");
            return;
        }

        String emailCode2=emailCode.getTitle1();
        if (TextUtils.isEmpty(emailCode2)){
            ToastUtil.show("请输入手机验证码");
            return;
        }


        Intent intent=new Intent(this,BPM_ModifyPswActivity.class);
        intent.putExtra(BPM_ModifyPswActivity.KEY_ACTION,BPM_ModifyPswActivity.MODIFY_PSW);
        intent.putExtra(BPM_ModifyPswActivity.KEY_CMD,Constant.IBpm.CMD_PHONE);
        intent.putExtra(BPM_ModifyPswActivity.KEY_EMAIL,phone);
        intent.putExtra(BPM_ModifyPswActivity.KEY_EMAILCODE,emailCode2);
        startActivity(intent);
    }

}
